import Vuex from 'vuex'
import Vue from 'vue'
import Axios from 'axios'


Vue.use(Vuex)

const store = new Vuex.Store({
	state: { 
		count: 1,
		users: [],
		logs: [],
		logsCount: 0
	},
	mutations: {
		increment(state) {
			state.count++;
		},
		addUser(state, users) {
			state.users.splice(0, state.users.length);
			state.users.push(...users)
		},
		addLog(state, logs) {
			state.logs.splice(0, state.logs.length);
			state.logs.push(...logs.list);
			state.logsCount = logs.recordCount;
			// console.log(logs);
		}
	},
	actions: {
		loadLogs({commit, state}, page) {
			console.log('page', page);
			// console.log('state', state);
			return new Promise((resolve, reject) => {
				//dist/interfacelog.json'
				Axios.get('http://localhost:4843/api/interfacelog', {params: page })
					.then(rsp => {
					commit('addLog', rsp.data);
					resolve()
				})
			})
		},
		fetchLogs({ commit, state }) {
			return new Promise((resolve, reject) => {
				Axios.get('http://localhost:4843/api/interfacelog').then(rsp => {
					// commit('addLog', rsp.data);
					resolve(rsp.data)
				})
			})
		}
	}
})

export default store